/*
COPYRIGHT (C) 2008 EPISERVER AB

THIS FILE IS PART OF WIKIX.
WIKIX IS FREE SOFTWARE: YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF THE GNU LESSER GENERAL PUBLIC LICENSE VERSION v2.1 AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION.

WIKIX IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU LESSER GENERAL PUBLIC LICENSE FOR MORE DETAILS.
YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC LICENSE ALONG WITH WIKIX. IF NOT, SEE <HTTP://WWW.GNU.ORG/LICENSES/>.
*/

using System;
using System.Web;
using EPiServer.Common.Visits;
using EPiServer.Core;
using EPiServer.Research.Wiki.Core;

namespace EPiServer.Research.Wiki.Pages
{
    public partial class Article : WikiPageBase
    {
        #region Member variables
        private string _baseUrl = string.Empty;
        #endregion
        #region Properties
        /// <summary>
        /// Gets the base URL.
        /// </summary>
        /// <value>The base URL.</value>
        private string baseUrl
        {
            get
            {
                if (_baseUrl == String.Empty)
                {
                    _baseUrl = CreateExternalLink(CurrentPage);
                    string separator = "?";
                    if (_baseUrl.Contains(separator))
                        _baseUrl += "&";
                    else
                        _baseUrl += "?";
                }
                return _baseUrl;
            }
        }
        /// <summary>
        /// Gets the RSS link.
        /// </summary>
        /// <value>The RSS link.</value>
        public string RSSLink
        {
            get
            {
                return baseUrl + "feed=RSS";
            }
        }

        /// <summary>
        /// Gets the ATOM link.
        /// </summary>
        /// <value>The ATOM link.</value>
        public string ATOMLink
        {
            get
            {
                return baseUrl + "feed=ATOM";
            }
        }
        #endregion

        #region method overrides
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (!string.IsNullOrEmpty(Request["editCallback"]))
            {
                EditManager.Instance.GetLock(PageReference.Parse(Request["editCallback"]));
                Response.Write("");
                Response.End();
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if ((!string.IsNullOrEmpty(Request["mode"])) && (Request["mode"].ToLower() == "talk"))
            {
                this.Title = Translate("/wiki/general/discussionsectiontitle") + CurrentPage.PageName;
                MainPlaceHolder.Controls.Add(Page.LoadControl("~/Templates/WikiX/Units/ArticleDiscussion.ascx"));
                LeftMenu1.MenuProperty = "LeftMenuDiscussion";

            }
            else if ((!string.IsNullOrEmpty(Request["mode"])) && (Request["mode"].ToLower() == "history"))
            {
                this.Title = Translate("/wiki/general/historysectiontitle") + CurrentPage.PageName;
                MainPlaceHolder.Controls.Add(Page.LoadControl("~/Templates/WikiX/Units/ArticleHistory.ascx"));
                LeftMenu1.MenuProperty = "LeftMenuHistory";
            }
            else
            {
                this.Title = CurrentPage.PageName;
                MainPlaceHolder.Controls.Add(Page.LoadControl("~/Templates/WikiX/Units/ArticleContents.ascx"));
                LeftMenu1.MenuProperty = "LeftMenu";

                if (CurrentPage.QueryDistinctAccess(EPiServer.Security.AccessLevel.Edit))
                {
                    if ((!string.IsNullOrEmpty(Request["edit"])) || (!string.IsNullOrEmpty(Request["add"])))
                    {
                        //In edit mode - load edit control
                        MainPlaceHolder.Controls.Add(Page.LoadControl("~/Templates/WikiX/Units/FullEdit.ascx"));
                    }
                }
                if (CurrentPageEntity != null)
                {
                    ltArticle.Entity = CurrentPageEntity;
                    //txtArticleTags.Text = CurrentPageEntity.Tags.GetTextBoxString();

                    HttpCookie visitCookie = Request.Cookies["visit"];

                    if (visitCookie == null)
                    {
                        visitCookie = new HttpCookie("visit");
                        visitCookie.Values[CurrentPage.PageGuid.ToString()] = CurrentPage.PageGuid.ToString();
                        visitCookie.Expires = DateTime.Now.AddDays(5);
                        Response.Cookies.Add(visitCookie);
                        VisitHandler.AddVisit(new Visit(CurrentPageEntity));
                    }
                    else
                    {
                        if (visitCookie.Values[CurrentPage.PageGuid.ToString()] == null)
                        {
                            visitCookie.Values[CurrentPage.PageGuid.ToString()] = CurrentPage.PageGuid.ToString();
                            visitCookie.Expires = DateTime.Now.AddDays(5);
                            Response.Cookies.Add(visitCookie);
                            VisitHandler.AddVisit(new Visit(CurrentPageEntity));
                        }
                    }
                }
                phTags.DataBind();
            }
        }

        protected override PageDataCollection FeedSource
        {
            get
            {
                return GetFeedPages();
                //return Filters.FilterForVisitor.Filter(DataFactory.Instance.GetChildren(CurrentPage.PageLink));
            }
        }
        #endregion

        #region Private Methods
        private PageDataCollection GetFeedPages()
        {
            PageDataCollection pages = new PageDataCollection();
            pages.Add(DataFactory.Instance.GetChildren(CurrentPage.PageLink));
            pages.Add(CurrentPage);
            return Filters.FilterForVisitor.Filter(pages);
        }
        #endregion
        //protected void btnSave_Click(object sender, EventArgs e)
        //{
        //    if (Page.IsValid && Request.IsAuthenticated)
        //    {
        //        // Add the new tags
        //        if (CurrentPageEntity != null)
        //        {
        //            WikiArticlePageEntity wikiArticlePageEntityCloned = CurrentPageEntity.Clone() as WikiArticlePageEntity;
        //            if (wikiArticlePageEntityCloned != null)
        //            {
        //                wikiArticlePageEntityCloned.EntityTags.SelectiveAdd(txtArticleTags.GetTags(), CurrentUser);
        //                PageEntityHandler.UpdatePageEntity(wikiArticlePageEntityCloned);
        //                CurrentPageEntity = null;
        //            }
        //        }
        //    }
        //}
    }
}
